package com.example.musicproject.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.pojo.Musicinfo;
import com.example.common.pojo.musicarticle;
import com.example.common.utils.R;
import com.example.service.Musicinfoservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/musicInfo")
@Slf4j
@Api(tags = "首页")
public class HomepageController {

    @Autowired
    private Musicinfoservice musicinfoservice;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/page")
    @ApiOperation(value = "音乐列表", notes = "音乐列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int", paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "int", paramType = "query",example = "10"),
            @ApiImplicitParam(name = "name", value = "音乐名", required = false, dataType = "String", paramType = "query",example= "音乐"),
    })
    public R<List<Musicinfo>> musicpage(@RequestHeader("token") String token, int page, int pageSize, String name){
        Page page1 = new Page(page,pageSize);
        LambdaQueryWrapper<Musicinfo> wapper = new LambdaQueryWrapper<>();
        wapper.like(name !=null ,Musicinfo::getName,name);
        musicinfoservice.page(page1, wapper);
        List<Musicinfo> records = page1.getRecords();
        List<Musicinfo> forlist = musicinfoservice.forlist(token, records);
        return R.success(forlist);
    }

    @PostMapping("/hot")
    @ApiOperation(value = "热门音乐", notes = "热门音乐")
    public R<List<Musicinfo>> musichotpage(@RequestHeader("token") String token){
        LambdaQueryWrapper<Musicinfo> wapper = new LambdaQueryWrapper<>();
        wapper.orderByDesc(Musicinfo::getAudition);
        wapper.last("limit 10");
        List<Musicinfo> list = musicinfoservice.list(wapper);

        List<Musicinfo> forlist = musicinfoservice.forlist(token, list);

        return R.success(forlist);
    }

    @PostMapping("/new")
    @ApiOperation(value = "最新音乐", notes = "最新音乐")
    public R<List<Musicinfo>> musicnewpage(@RequestHeader("token") String token){
        LambdaQueryWrapper<Musicinfo> wapper = new LambdaQueryWrapper<>();
        wapper.orderByDesc(Musicinfo::getCreateTime);
        wapper.last("limit 10");
        List<Musicinfo> list = musicinfoservice.list(wapper);

        List<Musicinfo> forlist = musicinfoservice.forlist(token, list);

        return R.success(forlist);
    }

//    点击收藏
    @GetMapping("/collection/{userid}/{musicinfoid}")
    @ApiOperation(value = "收藏", notes = "收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "Long", paramType = "path",example = "1"),
            @ApiImplicitParam(name = "musicinfoid", value = "音乐id", required = true, dataType = "Long", paramType = "path",example = "1")
    })
    public R<String> collectionid(@PathVariable("userid")Long userId ,@PathVariable("musicinfoid") Long musicinfoId){
        boolean b = musicinfoservice.storeUserTrends(musicinfoId, userId);
        if (b){
            return R.success("收藏成功");
        }
        return R.error("不可重复收藏");
    }

//    取消收藏
    @GetMapping("/uncollection/{userid}/{musicinfoid}")
    @ApiOperation(value = "取消收藏", notes = "取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true, dataType = "Long", paramType = "path",example = "1"),
            @ApiImplicitParam(name = "musicinfoid", value = "音乐id", required = true, dataType = "Long", paramType = "path",example = "1")
    })
    public R<String> uncollectionid(@PathVariable("userid")Long userId ,@PathVariable("musicinfoid") Long musicinfoId){
        boolean b = musicinfoservice.unstoreUserTrends(musicinfoId, userId);
        if (b){
            return R.success("取消收藏成功");
        }
        return R.error("不可重复取消");
    }

//    获取收藏歌曲id集合,去除redis缓存里面的list歌曲id集合，再在mapper内取出收藏歌曲的list信息
    @GetMapping("/collectioninfo")
    @ApiOperation(value = "收藏歌曲", notes = "收藏歌曲")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header",example = "token")
    })
    public R<List<musicarticle>> collectioninfo(@RequestHeader("token") String token){
        List<musicarticle> musicarticlelists = musicinfoservice.collectservice(token);
        return R.success(musicarticlelists);
    }

    @GetMapping("/viewcounts/{musicinfoid}")
    @ApiOperation(value = "浏览量", notes = "浏览量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "musicinfoid", value = "音乐id", required = true, dataType = "Long", paramType = "path",example = "1")
    })
    public R<String> viewcounts(@PathVariable("musicinfoid") Long musicinfoId){
        Double isnewScore = stringRedisTemplate.opsForZSet().incrementScore("view", String.valueOf(musicinfoId), 1);
        return R.success("浏览量已增加！");
    }

}
